﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using TFSTools.TFSLibrary;
using TFSTools.TFSLibrary.TestManagement;

namespace TFSTools.TestManagement.TestCaseReview
{
    public class WorkItemSorter : IComparable
    {
        private readonly TFSWorkItem workItem;
        private readonly string workItemComparer;

        public WorkItemSorter(TFSWorkItem workItem)
        {
            this.workItem = workItem;
            this.workItemComparer = workItem.Title;
        }

        public TFSWorkItem WorkItem
        {
            get { return this.workItem; }
        }

        #region IComparable Members

        public int CompareTo(object obj)
        {
            WorkItemSorter other = obj as WorkItemSorter;
            if (other == null)
            {
                throw new Exception("The method or operation is not implemented.");
            }
            else
            {
                return string.Compare(this.workItemComparer, other.workItemComparer);
            }
        }

        #endregion

        public static List<TFSWorkItem> Sort(TFSWorkItemCollection list)
        {
            var sorterList = new List<WorkItemSorter>(list.Count);
            foreach (var workItem in list)
            {
                sorterList.Add(new WorkItemSorter(workItem));
            }

            sorterList.Sort();

            var sortedList = new List<TFSWorkItem>(list.Count);
            sorterList.ForEach(workItem => sortedList.Add(workItem.WorkItem));
            return sortedList;
        }
    }
}
